package A_review.review9_13;

public class Class2698 {

//TODO:2023/9/13  惩罚数

    public int punishmentNumber(int n) {
 int ans=0;
        for (int i = 1; i <=n ; i++) {
            int t=i*i;
            if (dfs(t+"",0,i)){
                ans+=t;
            }
        }
return  ans;
    }

    public  boolean   dfs(String s,int start,int target){
        if (start==s.length()){
            return  target==0;

        }

        for (int i = start; i <s.length() ; i++) {
            int curr = Integer.parseInt(s.substring(start, i + 1));
            if (target-curr<0){
                break;
            }
            if (dfs(s,i+1,target-curr)){
                return true;
            }
        }
        return   false;
    }
}
